<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Redirecting...</title>
</head>
<body>
    <noscript>Failed to redirect. Javascript is disabled on your browser.</noscript>

    <div id="redirection-output" hidden>
        Redirecting...
    </div>
    
    <div id="url-generator" hidden>
        <p>
            No redirection request detected. Switched to url-generator mode.
        </p>
        <p>
            Please input the filename:
            <input id="url-generator-input" type="text" value="index.html">
            <div id="url-generator-output">index.html</div>
        </p>
    </div>

    <script>
        const redirectionOutputDiv = window.document.getElementById("redirection-output");
        redirectionOutputDiv.hidden = false;
        
        function doNavigation()
        {
            const url = new URL(window.location.href);
            const param = url.searchParams.get("f");
            if (param)
            {
                const newUrl = new URL(".", url);
                window.location.assign(new URL(param, newUrl));
            }
            else
            {
                const urlGeneratorInputDiv = window.document.getElementById("url-generator-input");
                const urlGeneratorOutputDiv = window.document.getElementById("url-generator-output");
                urlGeneratorInputDiv.addEventListener("input", e =>
                {
                    const newUrl = new URL(url);
                    newUrl.searchParams.append("f", e.target.value);
                    urlGeneratorOutputDiv.innerText = newUrl.href;
                });
                urlGeneratorInputDiv.dispatchEvent(new Event("input"));

                const urlGeneratorDiv = window.document.getElementById("url-generator");
                redirectionOutputDiv.hidden = true;
                urlGeneratorDiv.hidden = false;
            }
        }
        window.navigator.serviceWorker.register("./service-worker.js")
            .then(doNavigation)
            .catch(() => alert("Failed to register the service worker. The pages might be outdated due to unexpected caches."));

    </script>
</body>
</html>